package Enum;

import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: whh
 * @Date: 2021/5/31
 * @Description:
 */
public class Pizza {
    private PizzaStatus status;

    //状态为还没配送的pizza的enumset集合
    private static EnumSet<PizzaStatus> undeliveredPizzaStatuses = EnumSet.of(PizzaStatus.ORDERED,PizzaStatus.READY);

    private static EnumSet<PizzaStatus> deliveredPizzaStatuses = EnumSet.of(PizzaStatus.DELIVERED);

    public PizzaStatus getStatus() {
        return status;
    }

    public void setStatus(PizzaStatus status) {
        this.status = status;
    }

    public enum PizzaStatus{
        ORDERED(5){
            @Override
            public boolean isOrdered(){
                return true;
            }
        },
        READY (2){
            @Override
            public boolean isReady() {
                return true;
            }
        },
        DELIVERED (0) {
            @Override
            public boolean isDelivered() {
                return true;
            }
        };

        private int timeToDelivery;

        public boolean isOrdered() {return false;}

        public boolean isReady() {return false;}

        public boolean isDelivered(){return false;}

        public int getTimeToDelivery() {
            return timeToDelivery;
        }

        PizzaStatus (int timeToDelivery) {
            this.timeToDelivery = timeToDelivery;
        }
    }

    public boolean isDeliverable() {
        return this.status.isReady();
    }

    public void printTimeToDeliver() {
        System.out.println("Time to delivery is " +
          this.getStatus().getTimeToDelivery());
    }

    public static  List<Pizza> getAllUndeliveredPizzas(List<Pizza> input){
        //return input.stream().filter(s -> undeliveredPizzaStatuses.contains(s.getStatus())).collect(Collectors.toList());
        return input.stream().filter(s -> !deliveredPizzaStatuses.contains(s.getStatus())).collect(Collectors.toList());
    }

    public static EnumMap<Pizza.PizzaStatus,List<Pizza>> groupPizzaByStatus(List<Pizza> pizzaList){
        EnumMap<Pizza.PizzaStatus,List<Pizza>> pzByStatus = new EnumMap<PizzaStatus, List<Pizza>>(Pizza.PizzaStatus.class);
        //Iterator<Pizza> iterator = pizzaList.iterator();
        //while (iterator.hasNext()) {
        //    Pizza pz = iterator.next();
        //    PizzaStatus status = pz.getStatus();
        //    if (pzByStatus.containsKey(status)) {
        //      pzByStatus.get(status).add(pz);
        //    } else {
        //      List<Pizza> newPzList = new ArrayList<>();
        //      newPzList.add(pz);
        //      pzByStatus.put(status, newPzList);
        //    }
        //}
        pizzaList.forEach(s -> {
            PizzaStatus status = s.getStatus();
            if(pzByStatus.containsKey(status)){
                pzByStatus.get(status).add(s);
            } else {
                List<Pizza> newPzList = new ArrayList<>();
                newPzList.add(s);
                pzByStatus.put(status, newPzList);
            }
        });
        return pzByStatus;
    }
}
